<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/8/27
 * Time: 10:38
 */

namespace Biz\validate\activity;

use Biz\service\Sku as sSku;
use Biz\module\Classes\mgt\MealSave as mgtMealSave;

class MealInfo extends \Doraemon\pockets\validate\Base
{
    public static function rules()
    {
        return [

            ['meal_rebate', 'required', null, '部分商品没有设置套餐折扣，请设置后再提交', ['create', 'update']],
            ['meal_rebate', 'between', '1,10', '折扣需要大于1和小于等于10', ['create', 'update']],
            ['meal_rebate', 'callback', ['callback' => [new self(), 'checkPrice']], '商品的套餐价不能高于正常售价，请重新设置折扣', ['create','update']],
            //            ['meal_rebate', 'callback', ['callback' => [new self(), 'checkPrice']], mgtMealSave::$goodsId . '套餐价格不能大于商品原价', ['create','update']],

            ['sku_id', 'required', null, '部分商品没有设置主推SKU，请设置后再提交', ['create', 'update']],
        ];
    }

    public static function checkPrice($value, $valueIdentifier, $allData)
    {
        $skuInfo = sSku::Sku4Order([$allData['sku_id']]);
        $mealPrice = round(($skuInfo[$allData['sku_id']]['ori_price'] * $allData['meal_rebate']) / 10 );

        if($skuInfo[$allData['sku_id']]['ori_price'] < $mealPrice) {
            return false;
        }

        return true;
    }
}